package com.mc.moring.widge;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.mc.moring.R;
import com.mc.moring.bean.SingleSelectBean;
import com.mc.moring.util.DensityUtil;
import com.mc.moring.widge.adapter.SingleSelectAdapter;

import java.util.ArrayList;
import java.util.List;

public class SingleSelectMomenyDialogView extends PopupWindow {

    ArrayList<SingleSelectBean> itemList = new ArrayList<>();
    SingleSelectAdapter selectAdapter = null;
    View view;
    Context context;
    String title;
    private PopupWindow popupWindow;

    SingleSelectListener singleSelectListener;
    public SingleSelectMomenyDialogView(@NonNull Context context, View view, String title) {
        super(context);
        this.view = view;
        this.title = title;
        this.context = context;
    }
    public interface SingleSelectListener{
        void checked(SingleSelectBean s);
    }

    public void setSingleSelectListener(SingleSelectListener singleSelectListener){
        this.singleSelectListener = singleSelectListener;
    }
    public void refreshAdapter(SingleSelectBean bean){
        if(selectAdapter == null){
            return;
        }
        if(bean == null){
           return;
        }
        for (int i = 0; i < itemList.size(); i++) {
            if(bean.getId() == itemList.get(i).getId()){
                itemList.get(i).setText(bean.getText());
                itemList.get(i).setCheck(bean.isCheck());
            }else {
                itemList.get(i).setCheck(false);
            }
        }
        selectAdapter.notifyDataSetChanged();
    }

    public void showSingleSelectDialog(Context context,List<SingleSelectBean> dataList, int defaultIndex) {
        this.context = context;
        View popuView = View.inflate(context,R.layout.pop_select_money, null);
        popupWindow = new PopupWindow(popuView, DensityUtil.getScreenWidth((Activity) context), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha();
            }
        });
        itemList.clear();
        itemList.addAll(dataList);
        ZiLiPageWheelView wheelView = popuView.findViewById(R.id.zilipagewheelview);
        TextView tv_option_title = popuView.findViewById(R.id.tv_option_title);
        ImageView iv_close = popuView.findViewById(R.id.iv_close);

        wheelView.setDefault(defaultIndex);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        TextView confirm = popuView.findViewById(R.id.tv_confirm);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String value = wheelView.getSelectedText();
                for (int i = 0; i < itemList.size(); i++) {
                    if(TextUtils.equals(itemList.get(i).getText(),value)){
                        singleSelectListener.checked(itemList.get(i));
                    }
                }
                popupWindow.dismiss();
            }
        });
        wheelView.setData(itemList);
        tv_option_title.setText(title);
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        backgroundAlpha1();
    }
    private void backgroundAlpha1() {//透明函数
        WindowManager.LayoutParams lp = ((Activity)context).getWindow().getAttributes();
        lp.alpha = (float) 0.5;
        ((Activity)context).getWindow().setAttributes(lp);
    }

    private void backgroundAlpha() {//透明函数
        WindowManager.LayoutParams lp = ((Activity)context).getWindow().getAttributes();
        lp.alpha = (float) 1.0;
        ((Activity)context).getWindow().setAttributes(lp);
    }
    public void dismiss(){
        super.dismiss();
    }
}
